/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：ArticlesContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseArticleInfo;
import com.xteam.ecsystem.service.admin.ArticlesService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 文章信息控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class ArticlesContorller {

	@Autowired
	private ArticlesService baiService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getBaiList.do")
	public void getBaiList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String baiGroup = ReqHelper.getRequestString(request, "baiGroup");
		String baiTitle = ReqHelper.getRequestString(request, "baiTitle");
		String baiState = ReqHelper.getRequestString(request, "baiState");
		
		String strList = this.baiService.getBaiList(pageIndex, pageSize, 
				baiTitle, baiGroup, baiState);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateBai.do")
	public void saveOrUpdateBai(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "articleInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseArticleInfo bai = (BaseArticleInfo)JSONObject.toBean(obj, BaseArticleInfo.class);
		
		if(StringUtil.isNotNullOrEmpty(bai.getBaiId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ARTICLES_MGR, 
				"saveOrUpdateObject", lslType, "BaseArticleInfo");
		
		this.baiService.saveOrUpdateObject(bai, bai.getBaiId());
	}
	
	@RequestMapping("/getBaiById.do")
	public void getBaiById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String baiId = ReqHelper.getRequestString(request, "baiId");		
		String strInfo = this.baiService.getBaiById(baiId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateBaiStateById.do")
	public void updateBaiStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String baiId = ReqHelper.getRequestString(request, "baiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.baiService.updateBaiStateById(baiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ARTICLES_MGR, 
				"updateBaiStateById", ConstantLocation.LOG_Edit_STATE, "BaseArticleInfo");

	}
	
	@RequestMapping("/delBaiImageById.do")
	public void delBaiImageById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String baiId = ReqHelper.getRequestString(request, "baiId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.baiService.delBaiImageById(baiId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ARTICLES_MGR, 
				"delBaiImageById", ConstantLocation.LOG_DELETE_PIC, "BaseArticleInfo");

	}
	
	@RequestMapping("/delBaiByIds.do")
	public void delBaiByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String baiIds = ReqHelper.getRequestString(request, "baiIds");	
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		this.baiService.delObject(baiIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ARTICLES_MGR, 
				"delObject", ConstantLocation.LOG_DELETE, "BaseArticleInfo");
	}
	
	@RequestMapping("/getBagTreeSelect.do")
	public void getBagTreeSelect(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String type = ReqHelper.getRequestString(request, "type");
		String bggTreeSelect = this.baiService.getBagTreeSelect(type);
		response.getWriter().write(bggTreeSelect);

	}
	
}
